<?php

namespace app\qsapi\controller;

use app\qsapi\model\BoxRechargeConfig;
use app\qsapi\model\BoxRechargeOrder;
use app\qsapi\model\BoxRechargePaymentAccount;
use app\qsapi\model\BoxRechargePayType;
use app\qsapi\model\BoxYunOrder;
use think\Controller;
use think\response\Json;


/**
 * 微信支付模块
 */
class WeixinPay extends Controller
{

    protected $noNeedLogin = ['weixinform', 'paysuccess','rechargepaysuccess','wxpayopenid','rechargepcpaysuccess'];

    /**
     * @Notes:微信支付
     * @Interface weixinForm
     * @return Json
     */
    public function weixinForm()
    {
        $params = $_REQUEST;
        $host   = \request()->domain();
        $this->assign('host', $host);
        $this->assign('order_num', $params['order_num']);
        $this->assign('pay_url', base64_decode($params['pay_url']));
        return $this->fetch('weixin_form');
    }

    /**
     * 云手机支付成功页面
     */
    public function paySuccess()
    {
        $params    = $_REQUEST;
        $orderInfo = BoxYunOrder::where(['order_num' => $params['order_num']])->find();
        if (intval($orderInfo['pay_status']) == 1) {
            return $this->fetch('pay_success');
        } else {
            $host = \request()->domain();
            $this->assign('host', $host);
            $this->assign('order_num', $params['order_num']);
            $pay_url      = base64_decode($params['pay_url']);
            $pay_url_data = $pay_url . '&redirect_url=' . urlencode($host . "/WeixinPay/paysuccess?order_num=" . $params['order_num'] . '&pay_url=' . base64_encode($pay_url));
            $this->assign('pay_url', $pay_url_data);
            return $this->fetch('weixin_form');
        }
    }

    /**
     * 充值支付成功页面
     */
    public function rechargePaySuccess()
    {
        $params    = $_REQUEST;
        $orderInfo = BoxRechargeOrder::where(['order_num' => $params['order_num']])->find();
        if (intval($orderInfo['pay_status']) == 1) {
            return $this->fetch('pay_success');
        } else {
            $host = \request()->domain();
            $this->assign('host', $host);
            $this->assign('order_num', $params['order_num']);
            $pay_url      = base64_decode($params['pay_url']);
            $pay_url_data = $pay_url . '&redirect_url=' . urlencode($host . "/WeixinPay/rechargepaysuccess?order_num=" . $params['order_num'] . '&pay_url=' . base64_encode($pay_url));
            $this->assign('pay_url', $pay_url_data);
            return $this->fetch('weixin_form');
        }
    }



    /*
     * 微信支付获取openid
     */
    public function wxPayOpenid()
    {
        $param    = $_REQUEST;
        $orderNumber = $param['order_num'];
        $openid      = $param['openid'] ?? '';

        $order     = new BoxRechargeOrder();
        $orderInfo = $order->where(['order_num' => $orderNumber])->find();
        if (empty($orderInfo)) {
            return message("订单不存在", false);
        }
        if (intval($orderInfo['pay_status']) == 1) {
            return message("订单已支付", false);
        }

        $payment_id              = BoxRechargePayType::where('pay_type', 'wxpay_qrcode')->value('payment_id');
        $paymentAccountData      = BoxRechargePaymentAccount::where('id', $payment_id)->find();
        $payConfig               = json_decode($paymentAccountData['params'], true);
        $url                     = \request()->domain();
        if (!$openid) {
            $appid        = $payConfig['appid'];
            $secret       = $payConfig['appsecret'];
            $wxJsapiOauthUrl = Env('domain.wx_jsapi_oauth_url');
            $path = urlencode( $url . '/WeixinPay/wxPayOpenid?order_num=' . $orderNumber);
            $wxUrl = $wxJsapiOauthUrl . '?path='.$path.'&appid='.$appid.'&secret='.$secret.'&type=1';
            header("Location:".$wxUrl);
        }
        $paySuccess  = $url . "/WeixinPay/rechargepcpaysuccess";
        $this->assign('pay_order_number', $orderNumber);
        $this->assign('openid', $openid);
        $this->assign('pay_success_html', $paySuccess);

        return $this->fetch('weixin_jsapi_pay');
    }

    /**
     * pc充值支付成功页面
     */
    public function rechargePcPaySuccess()
    {
        return $this->fetch('pay_success');
    }

}
